package com.liang.security.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Optional;

/**
 * @Author: liangjy
 * @Date:2022/2/5 22:28
 * @Package_name: com.liang.security.controller
 * @Description:
 */
@Controller
public class LoginController {
    /**
     * 自定义登录页
     * @return
     */
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    /**
     * 登录成功后的跳转页面
     * @return
     */
    @RequestMapping("/login-success")
    public String loginSuccess(){
        return "hello";
    }
    //r1资源
    @GetMapping("/r/r1")
    @ResponseBody
    public String r1(){
        return getUserName() + "访问r1资源";
    }
    //r2资源
    @GetMapping("/r/r2")
    @ResponseBody
    public String r2(){
        return getUserName() + "访问r2资源";
    }
    //z1资源
    @GetMapping("/z/z1")
    @ResponseBody
    public String z1(){
        return getUserName() + "访问z1资源";
    }
    /**
     * 获取当前登录人名称
     */
    public String getUserName(){
        //security完成认证之后，会通过sessionAuthenticaion将用户信息存储到SecurityContextHolder
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //获取用户信息
        Object principal = authentication.getPrincipal();
        //如果为空，则为匿名，不为空，返回
        String userName = Optional.ofNullable(principal).map(name -> {
            if (name instanceof UserDetails) {
                UserDetails userDetails = (UserDetails) name;
                return userDetails.getUsername();
            } else {
                return name.toString();
            }
        }).orElse("匿名");
        return userName;
    }
}
